package examples;

import simpleTaskDispatcher.model.TaskQueue;
import simpleTaskDispatcher.model.WorkersPool;
import simpleTaskDispatcher.model.tasks.impl.ContextFreeTask;

public class SingleTaskQueueExample {

	public static void main(String[] args) throws InterruptedException {
		//At the start of the program, create a workers pool
		WorkersPool pool = new WorkersPool();

		//The maximum number of concurrent tasks execution is the same as the number of processor
		pool.createWorkerForEachCPU();
		
		TaskQueue taskQueue1 = new TaskQueue("TaskFeed1");
		pool.addTaskQueue(taskQueue1);		


		//Within a task feed, tasks are executed in FIFO order. 
		//But it is possible to place a task as the next one using the 'putInPriority' method
		
		taskQueue1.put(new ContextFreeTask() {
			
			@Override
			public void execute() throws InterruptedException {
				//do something
			}
		});
		
		taskQueue1.put(new ContextFreeTask() {
			
			@Override
			public void execute() throws InterruptedException {
				//do something
			}
		});

		//Wait until these 2 tasks are executed, withoud blocking the main thread
		taskQueue1.barrier(false);

		//The main thread has not been blocked by the barrier, but this new task will be.
		taskQueue1.put(new ContextFreeTask() {
			
			@Override
			public void execute() throws InterruptedException {
				//do something
			}
		});
		
		//Close the task queue, when all tasks are executed, blocking the main thread during this time.
		taskQueue1.close(true);
		
		//...
		
		//Closing the worker pool before closing the application
		pool.closeWorkerPool();
	}
	
	
	
}
